/****************************************************************************
 *
 *   Copyright (c) 2021 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#ifndef ADSB_VEHICLE_HPP
#define ADSB_VEHICLE_HPP

#include <uORB/topics/transponder_report.h>
#include <uORB/topics/a02_dist_to_target.h>
#include <uORB/topics/a03_buoy_sate.h>
class MavlinkStreamADSBVehicle : public MavlinkStream
{
public:
	static MavlinkStream *new_instance(Mavlink *mavlink) { return new MavlinkStreamADSBVehicle(mavlink); }

	static constexpr const char *get_name_static() { return "ADSB_VEHICLE"; }
	static constexpr uint16_t get_id_static() { return MAVLINK_MSG_ID_ADSB_VEHICLE; }

	const char *get_name() const override { return get_name_static(); }
	uint16_t get_id() override { return get_id_static(); }
float a=1;
	bool const_rate() override { return true; }

	unsigned get_size() override
	{
		return _transponder_report_sub.advertised() ? MAVLINK_MSG_ID_ADSB_VEHICLE_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES : 0;
	}

private:
	explicit MavlinkStreamADSBVehicle(Mavlink *mavlink) : MavlinkStream(mavlink) {}

	uORB::Subscription _transponder_report_sub{ORB_ID(transponder_report)};
uORB::Subscription _a02_dist_to_target_sub{ORB_ID(a02_dist_to_target)};
uORB::Subscription _a03_buoy_sate_sub{ORB_ID(a03_buoy_sate)};
	bool send() override
	{
		bool sent = false;
a++;
        transponder_report_s pos;
        a02_dist_to_target_s _a02_dist_to_target;
        a03_buoy_sate_s _a03_buoy_sate;
        if(_transponder_report_sub.updated())
        {
            _transponder_report_sub.copy(&pos);
            _a02_dist_to_target_sub.copy(&_a02_dist_to_target);
            _a03_buoy_sate_sub.copy(&_a03_buoy_sate);
//        if (_transponder_report_sub.update(&pos)) {

//            if (!(pos.flags & transponder_report_s::PX4_ADSB_FLAGS_RETRANSLATE)) {
//                continue;
//            }

			mavlink_adsb_vehicle_t msg{};
            msg.ICAO_address = pos.icao_address;
            msg.lat = pos.lat * 1e7;
            msg.lon = pos.lon * 1e7;
            msg.altitude_type =1;
            msg.altitude =  _a02_dist_to_target.dist_to_target*1000;
            msg.heading = 0.0f;
            msg.hor_velocity = pos.hor_velocity;
            if(_a03_buoy_sate.state>4.9&&_a03_buoy_sate.state<5.1)
            {
                msg.ver_velocity = 5.0f;
            }
            else
            {
msg.ver_velocity = 0.0f;
            }

            pos.callsign[0]='N';
            pos.callsign[1]='1';
            pos.callsign[2]='2';
            pos.callsign[3]='3';
            pos.callsign[4]='4';
            pos.callsign[5]='5';
            pos.callsign[6]='0';
            pos.callsign[7]='0';
            pos.callsign[8]='\0';
			memcpy(&msg.callsign[0], &pos.callsign[0], sizeof(msg.callsign));
            msg.emitter_type = 7;
            msg.tslc =1;
            msg.squawk = 0;

            msg.flags = 87;

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_COORDS) { msg.flags |= ADSB_FLAGS_VALID_COORDS; }

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_ALTITUDE) { msg.flags |= ADSB_FLAGS_VALID_ALTITUDE; }

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_HEADING) { msg.flags |= ADSB_FLAGS_VALID_HEADING; }

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_VELOCITY) { msg.flags |= ADSB_FLAGS_VALID_VELOCITY; }

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_CALLSIGN) { msg.flags |= ADSB_FLAGS_VALID_CALLSIGN; }

			if (pos.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_SQUAWK) { msg.flags |= ADSB_FLAGS_VALID_SQUAWK; }

			mavlink_msg_adsb_vehicle_send_struct(_mavlink->get_channel(), &msg);
			sent = true;
      }

		return sent;
    }

};

#endif // ADSB_VEHICLE_HPP
